﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using UDS.Components;

namespace UDS.SubModule.UnitiveDocument.BBS
{
    /// <summary>
    /// Display 的摘要说明。
    /// </summary>
    public partial class DisplayPage : Page
    {
        private int _itemId;//贴子ID
        private int _boardId;//板块id
        private string _title = "";
        private bool _isBoardMaster;//是本版斑竹
        private string _userName;//用户名	

        protected bool IsBoardMaster
        {
            get { return _isBoardMaster; }
        }

        protected string UserName
        {
            get { return _userName == null ? string.Empty : _userName; }
        }

        protected string PageTitle
        {
            get { return _title; }
        }

        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                _userName = SiteContext.Current.UserName;
                _itemId = (Request.QueryString["ItemID"] == null) ? 0 : Int32.Parse(Request.QueryString["ItemID"].ToString());
                _boardId = (Request.QueryString["BoardID"] == null) ? 0 : Int32.Parse(Request.QueryString["BoardID"].ToString());

                ViewState["username"] = _userName;
                ViewState["itemid"] = _itemId;
                ViewState["boardid"] = _boardId;
                ViewState["isboardmaster"] = _isBoardMaster;

                BBSClass bbsclass = new BBSClass();
                BBSForumItem bbsforumitem = new BBSForumItem();
                bbsforumitem.ItemID = _itemId;
                SqlDataReader dr = null;
                dr = bbsclass.ReadBBSForumItem(bbsforumitem);

                //判断是否是斑竹
                _isBoardMaster = bbsclass.IsBoardMaster(_boardId, _userName);
                if (_isBoardMaster)
                    cbx_boardBulletin.Visible = true;
                else
                    cbx_boardBulletin.Visible = false;

                if (Request.Cookies["UDSBBSAdmin"] != null)
                {

                    if (Request.Cookies["UDSBBSAdmin"].Value == "1")
                        cbx_sysBulletin.Visible = true;
                    else
                        cbx_sysBulletin.Visible = false;
                }
                else
                {
                    cbx_sysBulletin.Visible = false;
                    cbx_sysBulletin.Visible = false;
                }


                while (dr.Read())
                {
                    this._title = lblTitle.Text = dr["title"].ToString();
                    sendtime.Text = dr["send_time"].ToString();
                    browsetime.Text = dr["hit_times"].ToString();
                    replaytimes.Text = dr["replay_times"].ToString();
                    sendman.Text = dr["sender"].ToString();
                    //判断是否是系统公告，如果是只有管理员能够操作
                    if (Boolean.Parse(dr["sysbulletin"].ToString()))
                    {
                        if (Request.Cookies["UDSBBSAdmin"] != null)
                        {
                            if (Request.Cookies["UDSBBSAdmin"].Value == "1")
                            {
                                itemcontent.InnerHtml += "<b>操作：</b><a href=javascript:window.open('DeleteItem.aspx?ItemID=" + _itemId + "&BoardID=" + _boardId + "','_self','');>删除此贴</a>|<a href='MoveItem.aspx?ItemID=" + _itemId + "'>移动帖子</a><br><hr color='#C0C0C0' size='1'>";
                                cbx_DeskTop.Visible = true;
                                cbx_sysBulletin.Checked = true;
                                if (Boolean.Parse(dr["DeskTop"].ToString()))
                                {
                                    cbx_DeskTop.Checked = true;
                                }
                                else
                                    cbx_DeskTop.Checked = false;
                            }
                            else
                            {
                                cbx_sysBulletin.Checked = false;
                            }
                        }

                    }
                    else
                    {
                        if ((Request.Cookies["UDSBBSAdmin"].Value == "1") || (_isBoardMaster))
                        {
                            itemcontent.InnerHtml += "<b>操作：</b><a href=javascript:window.open('DeleteItem.aspx?ItemID=" + _itemId + "&BoardID=" + _boardId + "','_self','');>删除此贴</a>|<a href='MoveItem.aspx?ItemID=" + _itemId + "'>移动帖子</a><br><hr color='#C0C0C0' size='1'>";
                            cbx_DeskTop.Visible = false;

                            if (Boolean.Parse(dr["bulletin"].ToString()))
                            {
                                cbx_boardBulletin.Checked = true;
                            }
                            else
                            {
                                cbx_boardBulletin.Checked = false;
                            }

                        }

                    }

                    itemcontent.InnerHtml += FormatTxt(UBB.txtMessage(dr["content"].ToString()));
                }
                dr.Close();
                BindData();

            }
            else
            {
                _userName = ViewState["username"].ToString();
                _itemId = Int32.Parse(ViewState["itemid"].ToString());
                _boardId = Int32.Parse(ViewState["boardid"].ToString());
                _isBoardMaster = Boolean.Parse(ViewState["isboardmaster"].ToString());
            }



        }

        private void BindData()
        {
            BBSClass bbs = new BBSClass();
            BBSForumItem item = new BBSForumItem();
            using (Database db = new Database())
            {
                item.ItemID = _itemId;
                using (SqlDataReader dr = bbs.ReadBBSForumItemReplay(item))
                {
                    replaylist.DataSource = dr;
                    replaylist.DataBind();
                    dr.Close();
                }
            }
        }


        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.cbx_DeskTop.CheckedChanged += new System.EventHandler(this.cbx_DeskTop_CheckedChanged);
            this.cbx_boardBulletin.CheckedChanged += new System.EventHandler(this.cbx_boardBulletin_CheckedChanged);
            this.cbx_sysBulletin.CheckedChanged += new System.EventHandler(this.cbx_sysBulletin_CheckedChanged);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion


        protected void cmdOK_ServerClick(object sender, System.EventArgs e)
        {
            BBSClass bbsclass = new BBSClass();
            BBSReplay replay = new BBSReplay();
            string replaycontent = ViewState["Content"] + Content.Value;
            replay.ItemID = _itemId;
            replay.Replayer = _userName;
            replay.ReplayIP = Request.ServerVariables["remote_addr"].ToString();
            replay.Content = replaycontent.Replace("<", "&lt");
            replay.Content = replay.Content.Replace(">", "&gt");
            try
            {
                replay.ReplayId = bbsclass.ReplayItem(replay);
                if (ViewState["filename"] != null && ViewState["filename"].ToString().Trim() != "")
                    replay.Attach(ViewState["filename"].ToString());
                BindData();
                Content.Value = "";
                ViewState["Content"] = "";
                ViewState["filename"] = "";
            }
            catch (Exception ex)
            {
                UDS.Components.Logger.Log(ex.ToString());
                Server.Transfer("../../Error.aspx");
            }

        }

        protected void DelReplay(object sender, System.EventArgs e)
        {
            BBSClass bbsclass = new BBSClass();
            BBSReplay replay = new BBSReplay();
            replay.ReplayId = Int32.Parse(((LinkButton)sender).CommandArgument.ToString());
            replay.ItemID = _itemId;
            try
            {
                replay.DelAttachment(Server.MapPath(".") + "\\Attachment\\");
                bbsclass.DelReplay(replay);
                BindData();
                Content.Value = "";
            }
            catch (Exception ex)
            {
                UDS.Components.Logger.Log(ex.ToString());
                Server.Transfer("../../Error.aspx");
            }
        }

        /// <summary>
        /// 上载文件
        /// </summary>
        private string UploadAtt()
        {
            HtmlForm FrmCompose = (HtmlForm)this.Page.FindControl("Display");
            UDS.Components.BBSClass bbs = new UDS.Components.BBSClass();

            string FileName = "";
            string Extension = "";
            string SavedName = "";
            string TotalSavedName = "";
            try
            {
                if (System.IO.Directory.Exists(Server.MapPath(".") + "\\Attachment"))
                {
                    for (int i = 0; i < FrmCompose.Controls.Count; i++)
                    {
                        if (FrmCompose.Controls[i].GetType().ToString() == "System.Web.UI.HtmlControls.HtmlInputFile")
                        {
                            HtmlInputFile hif = ((HtmlInputFile)(FrmCompose.Controls[i]));
                            if (hif.PostedFile.FileName.Trim() != "")
                            {
                                FileName = System.IO.Path.GetFileName(hif.PostedFile.FileName);
                                Extension = System.IO.Path.GetExtension(hif.PostedFile.FileName);

                                SavedName = bbs.InsertFile(FileName, Extension).ToString();
                                TotalSavedName += SavedName + Extension + ",";

                                hif.PostedFile.SaveAs(Server.MapPath(".") + "\\Attachment\\" + SavedName + Extension);
                            }
                            hif = null;
                        }
                    }
                    return (TotalSavedName);
                }
                else
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(".") + "\\Attachment");
                    UploadAtt();
                    return (TotalSavedName);
                }
            }
            catch (Exception ioex)
            {
                UDS.Components.Logger.Log(ioex.ToString());
                Server.Transfer("../../../Error.aspx");
                return (TotalSavedName);
            }

        }


        protected string FormatTxt(string content)
        {
            return (content.Replace(((char)13).ToString(), "<br>"));

        }

        private void btn_UpAtt_Click(object sender, System.EventArgs e)
        {
            string filename = UploadAtt();
            ViewState["filename"] += filename;
            string savedpath = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf("/")) + "/Attachment/";
            string[] arrfilename = filename.Split(',');
            string uploadcontent = ViewState["Content"] + Content.Value;
            switch (ddl_FileType.SelectedItem.Value)
            {
                case "picture":
                    for (int i = 0; i < arrfilename.Length; i++)
                    {
                        if (arrfilename[i].Trim() != "")
                        {
                            uploadcontent += " [IMG]" + savedpath + arrfilename[i] + "[/IMG]";
                        }
                    }
                    break;

                case "rm":
                    for (int i = 0; i < arrfilename.Length; i++)
                    {
                        if (arrfilename[i].Trim() != "")
                        {
                            uploadcontent += " [RM=320,240]" + savedpath + arrfilename[i] + "[/RM]";
                        }
                    }
                    break;

                case "mp3":

                case "avi":
                    for (int i = 0; i < arrfilename.Length; i++)
                    {
                        if (arrfilename[i].Trim() != "")
                        {
                            uploadcontent += " [MP=320,240]" + savedpath + arrfilename[i] + "[/MP]";
                        }
                    }
                    break;
                case "swf":
                    for (int i = 0; i < arrfilename.Length; i++)
                    {
                        if (arrfilename[i].Trim() != "")
                        {
                            uploadcontent += " [FLASH]" + savedpath + arrfilename[i] + "[/FLASH]";
                        }
                    }
                    break;
                case "other":
                    for (int i = 0; i < arrfilename.Length; i++)
                    {
                        if (arrfilename[i].Trim() != "")
                        {
                            uploadcontent += " [ATT]" + savedpath + arrfilename[i] + "[/ATT]";
                        }
                    }
                    break;
            }
            ViewState["Content"] = uploadcontent;
        }

        private void cbx_sysBulletin_CheckedChanged(object sender, System.EventArgs e)
        {
            UDS.Components.BBSClass bbs = new UDS.Components.BBSClass();
            BBSForumItem item = new BBSForumItem();
            item.ItemID = _itemId;
            bbs.ReadBBSForumItemStruct(item);

            if (((CheckBox)sender).Checked)
            {
                item.SysBulletin = true;
            }
            else
            {
                item.SysBulletin = false;
                item.DeskTop = false;
                cbx_DeskTop.Enabled = false;

            }

            bbs.ModBBSForumItem(item);
        }

        private void cbx_boardBulletin_CheckedChanged(object sender, System.EventArgs e)
        {
            UDS.Components.BBSClass bbs = new UDS.Components.BBSClass();
            BBSForumItem item = new BBSForumItem();
            item.ItemID = _itemId;
            bbs.ReadBBSForumItemStruct(item);

            if (((CheckBox)sender).Checked)
            {
                item.Bulletin = true;
            }
            else
            {
                item.Bulletin = false;
            }

            bbs.ModBBSForumItem(item);
        }

        private void cbx_DeskTop_CheckedChanged(object sender, System.EventArgs e)
        {
            UDS.Components.BBSClass bbs = new UDS.Components.BBSClass();
            BBSForumItem item = new BBSForumItem();
            item.ItemID = _itemId;
            bbs.ReadBBSForumItemStruct(item);

            if (((CheckBox)sender).Checked)
            {
                item.DeskTop = true;
            }
            else
                item.DeskTop = false;

            bbs.ModBBSForumItem(item);
        }

    }
}

